package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Notice;
import com.powernode.service.NoticeService;
import com.powernode.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@RequestMapping("shop/notice")
@Api(tags = "公告管理")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("page")
    @ApiOperation("分页查询公告列表")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    public ResponseEntity<Page<Notice>> loadNoticePage(Page<Notice> page, Notice notice) {
        Long shopId = AuthUtil.getUserIdOrShopId(false);
        notice.setShopId(shopId);
        Page<Notice> noticePage = noticeService.selectNoticePage(page, notice);
        return ResponseEntity.ok(noticePage);
    }


    @PostMapping
    @ApiOperation("新增公告")
    @PreAuthorize("hasAuthority('shop:notice:save')")
    public ResponseEntity<Void> addNotice(@RequestBody Notice notice) {
        Long shopId = AuthUtil.getUserIdOrShopId(false);
        notice.setShopId(shopId);
        noticeService.addNotice(notice);
        return ResponseEntity.ok().build();
    }

    @GetMapping("topNoticeList")
    @ApiOperation("查询前台置顶公告")
    public ResponseEntity<List<Notice>> loadFrontNotice() {
        List<Notice> noticeList = noticeService.selectFrontNotice();
        return ResponseEntity.ok(noticeList);
    }


}
